package glory.xxd

import cn.hutool.json.JSONUtil
import com.alibaba.fastjson.JSON
import com.thoughtworks.xstream.XStream;
import org.apache.commons.lang.StringUtils;
import org.jdom2.Document;
import org.jdom2.JDOMException;
import com.alibaba.fastjson.JSONObject;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder
import org.json.XML

import javax.jms.TextMessage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by alany on 2018/7/10.
 */
public class XmlUtils {
    public static JSONObject xml2Json(String xmlStr) throws JDOMException, IOException {
        if (StringUtils.isEmpty(xmlStr)) {
            return null;
        }
        //xmlStr = xmlStr.replaceAll("\\\n", "");
        byte[] xml = xmlStr.getBytes("UTF-8");
        JSONObject json = new JSONObject();
        InputStream is = new ByteArrayInputStream(xml);
        SAXBuilder sb = new SAXBuilder();
        Document doc = sb.build(is);
        Element root = doc.getRootElement();
        json.put(root.getName(), iterateElement(root));

        return json;
    }

    private static JSONObject iterateElement(Element element) {
        List<Element> node = element.getChildren();
        JSONObject obj = new JSONObject();
        List list = null;
        for (Element child : node) {
            list = new LinkedList();
            String text = child.getTextTrim();
            if (StringUtils.isBlank(text)) {
                if (child.getChildren().size() == 0) {
                    continue;
                }
                if (obj.containsKey(child.getName())) {
                    list = (List) obj.get(child.getName());
                }
                list.add(iterateElement(child)); //遍历child的子节点
                obj.put(child.getName(), list);
            } else {
                if (obj.containsKey(child.getName())) {
                    Object value = obj.get(child.getName());
                    try {
                        list = (List) value;
                    } catch (ClassCastException e) {
                        list.add(value);
                    }
                }
                if (child.getChildren().size() == 0) { //child无子节点时直接设置text
                    obj.put(child.getName(), text);
                } else {
                    list.add(text);
                    obj.put(child.getName(), list);
                }
            }
        }
        return obj;
    }

    /**
     * java Object 转 XML
     * @auth glory.xxd 2019-03-15
     * @param obj
     */
    static parseObject2Xml(obj){
        def json = net.sf.json.JSONObject.fromObject(obj)
        def xml =  new net.sf.json.xml.XMLSerializer()
        xml =  xml.write(json)
        return  xml.replace("<o>","").replace("</o>","")
    }

    /**
     * 针对INPUTDATALIST  特殊处理
     * @param obj
     * @return
     */
    static parseObject2Xml_1(obj){
        def json = net.sf.json.JSONObject.fromObject(obj)
        def xml =  new net.sf.json.xml.XMLSerializer()
        xml =  xml.write(json)

        xml = xml.replace("<INPUTDATAINFO class=\"array\">","").replace("</INPUTDATAINFO>","")
        xml = xml.replace("<e class=\"object\">","<INPUTDATAINFO>").replace("</e>","</INPUTDATAINFO>")

        return  xml.replace("<o>","").replace("</o>","")
    }

     static  json2xml(obj) {

         def json = JSONUtil.parseObj(obj)
         def xml = cn.hutool.json.XML.toXml(json)

         return xml
    }

}